﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.Controls;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Data;
using System.Data.Common;
using SharpDiary.Core.Utility;

namespace SharpDiary.Task
{
    public partial class TaskListControl : ListControlBase
    {

        #region Constructors

        public TaskListControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods

        /// <summary>
        /// 显示所有任务。
        /// </summary>
        public override void ShowItems()
        {
            base.ShowItems();

            int summaryMaxTextLength = int.Parse(UserSettings.GetInstance().GetSettingValue("SummaryTextMaxLength", "20"));

            List<TaskInfo> tasks = TaskInfo.GetAllTasks();
            foreach (TaskInfo task in tasks)
            {
                TaskInfoItemControl item = new TaskInfoItemControl();
                item.SummaryMaxTextLength = summaryMaxTextLength;
                item.Task = task;

                AddItem(item);
            }
        }

        public override void ShowItems(string sql, params DbParameter[] parameters)
        {
            base.ShowItems(sql);

            int summaryMaxTextLength = int.Parse(UserSettings.GetInstance().GetSettingValue("SummaryTextMaxLength", "20"));

            if (string.IsNullOrEmpty(sql)) { ShowItems(); }
            else
            {
                List<TaskInfo> tasks = DbObjectMappingHelper.QueryObjectList<TaskInfo>(sql, parameters);
                foreach (TaskInfo task in tasks)
                {
                    TaskInfoItemControl item = new TaskInfoItemControl();
                    item.SummaryMaxTextLength = summaryMaxTextLength;
                    item.Task = task;

                    AddItem(item);
                }
            }
        }

        #endregion
    }
}
